package com.winter.client;

import com.winter.base.WinterResponse;
import com.winter.entity.CooperatorConf;
import com.winter.service.CooperatorConfService;
import com.winter.CooperatorDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.winter.mq.RocketMqProducer;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author Jinhao
 * Date: 2022/10/26 11:14
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class TestClient {

    private final CooperatorConfService cooperatorConfService;
    private final StringRedisTemplate stringRedisTemplate;
    private final MongoTemplate mongoTemplate;


    /**
     * 工程连接测试
     *
     * @return list
     */
    @GetMapping("/test/get/all")
    public List<CooperatorConf> getAll() {
        log.info("测试INFO：输出表中所有数据");
        log.error("测试ERROR：数据表中所有数据");
        return cooperatorConfService.findAll();
    }

    /**
     * 参数传递注解测试
     *
     * @param test 测试参数
     * @return string
     */
    @GetMapping("/deal/test/transmit/param")
    String testTransmitParam(String test) {
        log.info("测试feign接口间的参数传递。。。DEAL接收-{}-", test);
        return test;
    }

    /**
     * com.winter.mq 消息发送测试
     *
     * @param topic 主题
     * @param tag   标签
     * @param body  内容
     * @return {@link WinterResponse}
     */
    @GetMapping("/test/send/mq")
    public WinterResponse<?> sendMqTest(@RequestParam("topic") String topic,
                                        @RequestParam("tag") String tag,
                                        @RequestParam("body") String body) {
        RocketMqProducer.sendMessage(topic, tag, body);
        return WinterResponse.success();
    }

    /**
     * 测试tag
     *
     * @param body body
     */
    @PostMapping("/test/tag/return/deal")
    void dealTag(@RequestParam("body") String body) {
        log.info("测试TAG回来了 body-{}-", body);
    }

    /**
     * Redis 连接测试
     *
     * @return {@link WinterResponse}
     */
    @GetMapping("/test/redis/connection")
    public WinterResponse<?> testRedis() {
        stringRedisTemplate.opsForValue().set("TEST_REDIS_STRING_KEY", "JinHao");
        return WinterResponse.success();
    }

    /**
     * mongodb 连接测试
     *
     * @return {@link WinterResponse}
     */
    @GetMapping("/test/mongodb/connection")
    public WinterResponse<?> testMongodb() {
        CooperatorDTO cooperatorDTO = new CooperatorDTO();
        cooperatorDTO.setCooperatorId("C123484789797987");
        cooperatorDTO.setCooperatorName("金浩测试");
        cooperatorDTO.setCreateTime(new Date());
        mongoTemplate.save(cooperatorDTO);
        return WinterResponse.success();
    }
}
